package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.PresentExchangeMapper;
import com.jingyanzi.marketing.domain.PresentExchangeDo;
import com.jingyanzi.marketing.entity.MaPresentExchange;
import com.jingyanzi.marketing.entity.MaPresentExchangeExample;
import com.jingyanzi.marketing.entity.MaPresentExchangeExample.Criteria;
import com.jingyanzi.marketing.query.PresentExchangeQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IPresentExchangeService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maPresentExchangeService")
public class PresentExchangeServiceImpl extends BaseServiceImpl implements IPresentExchangeService {

	@Autowired
	private PresentExchangeMapper presentExchangeMapper;

	public ResultDO add(PresentExchangeDo maPresentExchangeDo) {
		MaPresentExchange presentExchange = new MaPresentExchange();

		ResultSupport result = BeanUtilsExtends.copy(presentExchange, maPresentExchangeDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		presentExchange.setCreatetime(Calendar.getInstance().getTime());
		presentExchange.setIsDelete(1);
		int r = -1;

		try {
			r = presentExchangeMapper.insert(presentExchange);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setSuccess(false);
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, presentExchange.getId());
		return result;
	}

	public ResultDO modifi(PresentExchangeDo maPresentExchangeDo) {
		MaPresentExchange presentExchange = new MaPresentExchange();

		ResultSupport result = BeanUtilsExtends.copy(presentExchange, maPresentExchangeDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = presentExchangeMapper.updateByPrimaryKeySelective(presentExchange);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaPresentExchange presentExchange = new MaPresentExchange();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		presentExchange.setId(id);
		presentExchange.setIsDelete(-1);
		int r = -1;

		try {
			r = presentExchangeMapper.updateByPrimaryKeySelective(presentExchange);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		MaPresentExchange presentExchange = new MaPresentExchange();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			presentExchange = presentExchangeMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}

		if (presentExchange == null) {
			result.setErrorCode(ResultDO.PRESENTEXCHANGE_GET_ERROR);
			result.setErrorMsg("不存在该礼品兑换信息");
			result.setSuccess(false);
			return result;
		}
		PresentExchangeDo presentExchangeDo = getMaPresentExchangeDo(presentExchange);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, presentExchangeDo);
		return result;
	}

	public ResultDO query(PresentExchangeQuery maPresentExchangeQuery) {
		ResultSupport result = new ResultSupport();

		List<MaPresentExchange> list = null;

		MaPresentExchangeExample example = new MaPresentExchangeExample();
		Criteria createCriteria = example.createCriteria();
		if (StringUtils.isNotBlank(maPresentExchangeQuery.getPresentName())) {
			createCriteria.andPresentNameEqualTo(maPresentExchangeQuery.getPresentName());
		}
		if (StringUtils.isNotBlank(maPresentExchangeQuery.getMember())) {
			createCriteria.andMemberEqualTo(maPresentExchangeQuery.getMember());
		}
		if (maPresentExchangeQuery.getIdentifier() != null) {
			createCriteria.andIdentifierEqualTo(maPresentExchangeQuery.getIdentifier());
		}
		if (maPresentExchangeQuery.getPresentId() != null) {
			createCriteria.andPresentIdEqualTo(maPresentExchangeQuery.getPresentId());
		}
		createCriteria.andIsDeleteEqualTo(1);

		List<PresentExchangeDo> presentExchangeDos = null;

		if (maPresentExchangeQuery.isPage()) {
			example.setLimitByPage(maPresentExchangeQuery.getStart() + "," + maPresentExchangeQuery.getPageRows());
			long count = presentExchangeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = presentExchangeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取礼品兑换信息失败");
			result.setSuccess(false);
			return result;
		}
		presentExchangeDos = getMaPresentExchangeDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, presentExchangeDos);

		return result;
	}

}
